package com.sundablog.service.impl.systemsetup.permissionsManage.permissions;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundablog.mapper.AdminPermissionMapper;
import com.sundablog.mapper.AdminRolePermissionMapper;
import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminPermissionExample;
import com.sundablog.pojo.AdminPermissionExample.Criteria;
import com.sundablog.pojo.AdminRolePermission;
import com.sundablog.pojo.AdminRolePermissionExample;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.MenuResult;
import com.sundablog.result.ZTreeResult;
import com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService;

import cn.hutool.json.JSONUtil;



/**
 * 权限接口实现类
 * 
 * @ClassName: PermissionsServiceImpl
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: 哒哒
 * @date: 2018年1月31日 下午2:09:59
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class PermissionsServiceImpl implements PermissionsService {

	@Autowired
	private AdminPermissionMapper adminPermissionMapper;

	@Autowired
	private AdminRolePermissionMapper adminRolePermissionMapper;
	/**
	 * 添加一条权限 Title: addPermissions Description:
	 * 
	 * @param adminPermission
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#addPermissions(com.sundablog.pojo.AdminPermission)
	 */
	@Override
	public int addPermissions(AdminPermission adminPermission) {
		adminPermission.setCtime(new Date());
		int i = adminPermissionMapper.insert(adminPermission);
		return i;
	}

	/**
	 * 禁用一条权限 Title: updataDisablePermissions Description:
	 * 
	 * @param permissionId
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#updataDisablePermissions(int)
	 */
	@Override
	public int updataDisablePermissions(int permissionId) {
		AdminPermission adminPermission = adminPermissionMapper.selectByPrimaryKey(permissionId);
		adminPermission.setStatus(0);
		int i = adminPermissionMapper.updateByPrimaryKey(adminPermission);
		return i;
	}

	/**
	 * 查询全部权限返回ZTree Title: selectPermissions Description:
	 * 
	 * @return
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#selectPermissions()
	 */
	@Override
	public List<ZTreeResult> selectPermissions() {
		AdminPermissionExample example = new AdminPermissionExample();
		List<AdminPermission> list = adminPermissionMapper.selectByExample(example);
		List<ZTreeResult> zTreeResults = new ArrayList<>();
		for (AdminPermission adminPermission : list) {
			ZTreeResult zTreeResult = new ZTreeResult();
			zTreeResult.setId(adminPermission.getPermissionId());
			zTreeResult.setpId(adminPermission.getPid());
			zTreeResult.setName(adminPermission.getName());
			zTreeResults.add(zTreeResult);
		}
		return zTreeResults;
	}

	/**
	 * 分页查询
	 * Title: findPagingQuery   
	 * Description:    
	 * @param page
	 * @param limit
	 * @param pid
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#findPagingQuery(int, int, int)
	 */
	@Override
	public LayUiResult findPagingQuery(int page, int limit, int pid) {
		PageHelper.startPage(page, limit);
		AdminPermissionExample example = new AdminPermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(pid);
		List<AdminPermission> list = adminPermissionMapper.selectByExample(example);
		PageInfo<AdminPermission> pageInfo = new PageInfo<>(list);
		LayUiResult layUiResult = new LayUiResult();
		layUiResult.setCode(0);
		layUiResult.setMsg("");
		layUiResult.setCount(pageInfo.getTotal());
		layUiResult.setData(list);
		return layUiResult;
	}

	/**
	 * 根据id查询一条权限数据
	 * Title: selectPermissionsById   
	 * Description:    
	 * @param permissionId
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#selectPermissionsById(int)
	 */
	@Override
	public AdminPermission selectPermissionsById(int permissionId) {
		AdminPermission permission = adminPermissionMapper.selectByPrimaryKey(permissionId);
		return permission;
	}

	/**
	 * 修改一条数据
	 * Title: updataPermissions   
	 * Description:    
	 * @param adminPermission
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#updataPermissions(com.sundablog.pojo.AdminPermission)
	 */
	@Override
	public int updataPermissions(AdminPermission adminPermission) {
		int i = adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);
		return i;
	}

	/**
	 * 根据ID修改状态
	 * Title: updataPermissionsByStatus   
	 * Description:    
	 * @param id
	 * @return   
	 * @see com.sundablog.service.systemsetup.permissionsManage.permissions.PermissionsService#updataPermissionsByStatus(int)
	 */
	@Override
	public int updataPermissionsByStatus(int id) {
		AdminPermission permission = adminPermissionMapper.selectByPrimaryKey(id);
		if (permission.getStatus() == 1) {
			permission.setStatus(0);
		} else {
			permission.setStatus(1);
		}
		int i = adminPermissionMapper.updateByPrimaryKey(permission);
		return i;
	}

	@Override
	public List<String> selectByRoleId(int roleId) {
		AdminRolePermissionExample example = new AdminRolePermissionExample();
		com.sundablog.pojo.AdminRolePermissionExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<AdminRolePermission> list = adminRolePermissionMapper.selectByExample(example);
		List<String> list2 = new ArrayList<>();
		for (AdminRolePermission adminRolePermission : list) {
			AdminPermission adminPermission = adminPermissionMapper.selectByPrimaryKey(adminRolePermission.getPermissionId());
			if (!("".equals(adminPermission.getPermissionValue()))) {
				list2.add(adminPermission.getPermissionValue());
			}
			
			
		}
		return list2;
	}
}
